﻿using LCL;
using LCL.CodeHelper;
using LCL.Config;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace LCL.IBuilder
{
    public class BaseProjectBuilder
    {
        #region 属性
        public string Name { get; set; }
        public string NameSpace { get; set; }
        public string TableFormat { get; set; }
        public string ConnectionString { get; set; }
        public string DbHost { get; set; }
        public string DbName { get; set; }
        public LDbType DbType { get; set; }
        public string DbUser { get; set; }
        public string DbPwd { get; set; }
        public DbAccesser DbHelper
        {
            get
            {
                return DBOMaker.GetDbAccesser(ConnectionString, DbType);
            }
        }
        public IDbObject DbObject
        {
            get
            {
                DbConfigInfo config = new DbConfigInfo();
                config.DbHost = this.DbHost;
                config.DbType = this.DbType;
                config.DbUser = this.DbUser;
                config.DbPwd = this.DbPwd;
                return DBOMaker.GetDbObject(config);
            }
        }
        #endregion
        public bool IsHasIdentity(List<ColumnInfo> _keys)
        {
            bool isid = false;
            if (_keys.Count > 0)
            {
                foreach (ColumnInfo key in _keys)
                {
                    if (key.IsIdentity)
                    {
                        isid = true;
                    }
                }
            }
            return isid;
        }
        public List<ColumnInfo> GetColumnList(string tableName)
        {
            return DbObject.GetColumnInfoList(DbName, tableName);
        }

        public virtual void CodeLib()
        {
            List<string> files = new List<string>();
            files.Add("LCL.Common.dll");
            files.Add("MySql.Data.dll");
            files.Add("Oracle.DataAccess.dll");
            files.Add("System.Data.SQLite.dll");

            string sourcDir = Utils.GetMapPath("") + @"/";
            string targetDir = CodeCommon.DesktopDirectory + NameSpace + @"/Lib/";
            CodeCommon.FolderCheck(CodeCommon.DesktopDirectory + NameSpace + @"/Lib/");

            for (int i = 0; i < files.Count; i++)
            {
                string file = files[i];
                File.Copy(sourcDir + file, targetDir + file, true);
            }
        }
        public virtual void MSBuild()
        {
            string dir = CodeCommon.DesktopDirectory + NameSpace;
            StringBuilder builder = new StringBuilder();
            #region msbuild
            //builder.AppendLine("@echo off ");
            //builder.AppendLine("cd C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\ ");
            //builder.AppendLine("msbuild "+dir+"\\Model\\"+NameSpace+".Model.csproj ");
            //builder.AppendLine("msbuild "+dir+"\\IDAL\\"+NameSpace+".IDAL.csproj ");
            //builder.AppendLine("msbuild "+dir+"\\DALFactory\\"+NameSpace+".DALFactory.csproj ");
            //builder.AppendLine("msbuild "+dir+"\\DAL\\"+NameSpace+".DAL.csproj ");
            //builder.AppendLine("msbuild "+dir+"\\"+NameSpace+".sln ");
            //builder.AppendLine("pause ");
            #endregion
            #region devenv.exe
            //D:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe
            string devenv = @"D:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe";
            builder.AppendLine("@echo off  ");
            builder.AppendLine("\"" + devenv + "\" " + dir + "\\Model\\" + NameSpace + ".Model.csproj /Build ");
            builder.AppendLine("\"" + devenv + "\" " + dir + "\\" + NameSpace + ".sln /Build ");
            builder.AppendLine("exit ");
            #endregion

            string filename = dir + @"/build.bat";
            CodeCommon.CreateFiles(filename, builder.ToString(), Encoding.ASCII);
            CmdHelper.RunBat(filename);
        }

        //格式化XML
        public string FormatXml(string sUnformattedXml)
        {
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(sUnformattedXml);
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlTextWriter xtw = null;
            try
            {
                xtw = new XmlTextWriter(sw);
                xtw.Formatting = Formatting.Indented;
                xtw.Indentation = 1;
                xtw.IndentChar = '\t';
                xd.WriteTo(xtw);
            }
            finally
            {
                if (xtw != null)
                    xtw.Close();
            }
            return sb.ToString();
        }
    }
}
